package com.aso114.project.mvp.activity;


import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.text.TextUtils;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.aso114.project.R;
import com.aso114.project.mvp.LoginSP;
import com.aso114.project.util.MySharedPreferences;
import com.aso114.project.util.SystemBarHelper;
import com.bumptech.glide.Glide;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * @{# SplashActivity.java Create on 2013-5-2 下午9:10:01
 * <p>
 * class desc: 启动画面 (1)判断是否是首次加载应用--采取读取SharedPreferences的方法
 * (2)是，则进入GuideActivity；否，则进入MainActivity (3)3s后执行(2)操作
 * <p>
 * <p>
 * Copyright: Copyright(c) 2013
 * </p>
 * @Version 1.0
 * @Author <a href="mailto:gaolei_xj@163.com">Leo</a> 启动的时候顺便获取定位
 */
public class StarActivity extends Activity {

    String firstrun = "";
    @BindView(R.id.starlogo)
    ImageView starlogo;
    @BindView(R.id.title_top_ly)
    ImageView titleTopLy;

    @BindView(R.id.tv_agreement)
    TextView tvAgreement;
    @BindView(R.id.layout_agreement)
    LinearLayout layoutAgreement;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    @BindView(R.id.tv_commit)
    TextView tvCommit;
    @BindView(R.id.cb_agreement)
    ImageView cbAgreement;
    @BindView(R.id.ch_delete)
    CheckBox chDelete;
    @BindView(R.id.explaintlayout)
    RelativeLayout explaint;
    private CheckBox cbAddress;
    private CheckBox cbGpsFirst;
    private TextView tvReult;
    int agree = 0;//0 没有勾选  1勾选
    private static final int GO_HOME = 1000;
    private static final int GO_GUIDE = 1001;
    // 延迟3秒
    private static final long SPLASH_DELAY_MILLIS = 3000;
    Intent intent;
    private static final String SHAREDPREFERENCES_NAME = "first_pref";

    /**
     * Handler:跳转到不同界面
     */
    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case GO_HOME:

                    goHome();
                    break;
                case GO_GUIDE:
                    goGuide();
                    break;
                default:
                    break;
            }
            super.handleMessage(msg);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.staractivity);
        ButterKnife.bind(this);
        SystemBarHelper.immersiveStatusBar(this, 0);
        SystemBarHelper.setHeightAndPadding(this, titleTopLy);
        Glide.with(this).load(R.mipmap.logogif).into(starlogo);
        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
            finish();
            return;
        }

        if (!LoginSP.getInstance().getFirstTost().equals("")) {
            explaint.setVisibility(View.GONE);
            init();
        }

        chDelete.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {
                // TODO Auto-generated method stub
                if (isChecked) {
                    System.out.println("选中");
                    agree = 1;
                } else {
                    System.out.println("取消选中");
                    agree = 0;
                }
            }
        });


    }


    private void init() {

        // 读取SharedPreferences中需要的数据
        // 使用SharedPreferences来记录程序的使用次数
        SharedPreferences preferences = getSharedPreferences(
                SHAREDPREFERENCES_NAME, MODE_PRIVATE);
        // 取得相应的值，如果没有该值，说明还未写入，用true作为默认值
        firstrun = MySharedPreferences.getInstance(StarActivity.this).getLoginLine();

        // 使用Handler的postDelayed方法，3秒后执行跳转到MainActivity
        mHandler.sendEmptyMessageDelayed(GO_HOME, SPLASH_DELAY_MILLIS);

    }

    private void goHome() {

        if (LoginSP.getInstance().getFirst().equals("")) {
            //表示第一次登陆 必须跳转去登陆
            intent = new Intent(StarActivity.this, LoginActivity.class);
            startActivity(intent);
        } else {
            if (TextUtils.isEmpty(LoginSP.getInstance().getToken())) {
                LoginSP.getInstance().setFirst("");
                intent = new Intent(StarActivity.this, LoginActivity.class);
                startActivity(intent);
            } else {
                intent = new Intent(StarActivity.this, MainActivity.class);
                startActivity(intent);
            }
        }

        finish();
    }

    private void goGuide() {
//		 Intent intent = new Intent(StarActivity.this, GuideActivity.class);
//		 StarActivity.this.startActivity(intent);
//		 StarActivity.this.finish();
    }


    @OnClick({R.id.tv_agreement, R.id.layout_agreement, R.id.tv_cancel, R.id
            .tv_commit})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case R.id.tv_agreement:

                intent = new Intent(StarActivity.this, MessageExplaintActivity.class);
                //AdvertisingActivity
                startActivity(intent);

                break;
            case R.id.layout_agreement:

                break;
            case R.id.tv_cancel:
                System.out.println("取消");
                finish();

                break;
            case R.id.tv_commit:
                System.out.println("进入");

                if (agree == 0) {


                    Toast toast = Toast.makeText(getApplicationContext(), "您还未同意个人信息保护条款", Toast
                            .LENGTH_SHORT);
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();


                } else {
                    LoginSP.getInstance().setFirstTost("1");
                    if (LoginSP.getInstance().getFirst().equals("")) {
                        //表示第一次登陆 必须跳转去登陆
                        intent = new Intent(StarActivity.this, LoginActivity.class);
                        //AdvertisingActivity
                        startActivity(intent);
                    } else {
                        intent = new Intent(StarActivity.this, MainActivity.class);
                        //AdvertisingActivity
                        startActivity(intent);
                    }

                    finish();

                }
                break;
        }
    }
}